/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package flex.messaging.services.remoting.adapters;

import flex.messaging.MessageException;
import flex.messaging.messages.Message;
import flex.messaging.messages.RemotingMessage;

/**
 *
 * @author fach
 */
public class EJBAdapter extends JavaAdapter {

	@Override
	public Object invoke(Message message) {
		try {
			return super.invoke(message);
		} catch(MessageException me) {
			System.out.println("me = "+me.getClass().getName());
			me.printStackTrace();
			RemotingMessage remotingMessage = (RemotingMessage)message;
			String methodName = remotingMessage.getOperation();
			String className = message.getDestination();
			Throwable cause = me.getRootCause();
			String msg = "";
			if(cause!=null) {
				if(cause.getClass().getName().equals("javax.ejb.EJBException")) cause = cause.getCause();
				me.setRootCause(cause);
				msg = cause.getMessage();
//				if(msg == null) msg = cause.getClass().getName();
//				me = new MessageException(msg);
			}
//			me.setCode(msg);
//			me.setRootCause(cause);
//			me.setStackTrace(me.getStackTrace());
//			me.setDetails(className+"."+methodName);
			throw me;
		}
	}

}
